﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DICE_GS;
using System.Text;

public class TeamCard : MonoBehaviour {
	public UISprite cardImage;
	public UILabel cardNameLabel;
	public UILabel cardAttack;
	public UILabel cardDefense;
	public UILabel cardMove;
	public UILabel cardHP;
	public GameObject[] cardStars;
	
	public UILabel cardStatLabel;
	
	public UISprite selectedCardEffect;
	public UISprite alreadySelectedCardMark;
 	
	
	[HideInInspector]
	public DeckEditSceneManager editManager;
	[HideInInspector]
	public DeckCardSelectSceneManager manager;
	[HideInInspector]
	public MyUnitListManager listManager;
	
	[HideInInspector]
	public int index;
	
	public int cardDBId
	{
		get
		{
			return _unitInfo.UnitDBID;
		}
	}
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	UnitInfo _unitInfo;
	public UnitInfo unitInfo
	{
		get
		{
			return _unitInfo;
		}
	}
	public void SetTeamCardInfos(UnitInfo unitInfo)
	{
		_unitInfo = unitInfo;
		cardImage.spriteName = unitInfo.spriteName;
		cardNameLabel.text = unitInfo.name;
//		cardAttack.text = unitInfo.attack.ToString();
//		cardDefense.text = unitInfo.defense.ToString();
//		cardMove.text = unitInfo.move.ToString();
		StringBuilder builder = new StringBuilder();
		builder.Append(CStringTableManager.Instance.GetItemById(1315));
		builder.Append(unitInfo.attack);
		builder.Append(" ");
		builder.Append(CStringTableManager.Instance.GetItemById(1319));
		builder.Append(unitInfo.defense);
		builder.Append(" ");
		builder.Append(CStringTableManager.Instance.GetItemById(1323));
		builder.Append(unitInfo.move);
//		cardStatLabel.text = " 
		cardHP.text = CStringTableManager.Instance.GetItemById(1613)+ " " + unitInfo.hp.ToString();
		
		CUnitData unitData = CUnitDataManager.Instance.GetUnitData(unitInfo.UnitClassID);
		for(int i=0; i<5; i++)
		{
			if( i < unitData.grade )
			{
				cardStars[i].SetActive(true);
			}
			else
				cardStars[i].SetActive(false);
		}
		
		if( CSceneManager.m_Scene == SCENE.UnitInvenScene )
			GetComponent<LTButtonMessage>().enabled = false;
	}
	
	public bool isSelected = false;
	void ClickedTeamCard()
	{
		if( CSceneManager.m_Scene == SCENE.UnitInvenScene )
		{
			if( !isSelected )
			{
				selectedCardEffect.gameObject.SetActive(true);
				alreadySelectedCardMark.gameObject.SetActive(true);
				manager.selectedCardObject.SetCardInfo(index, MyInfo.Instance.MyUnitsList[index]);
				manager.selectedCardObject.gameObject.SetActive(true);
				isSelected = true;
//				DeckInfo deckInfo = MyInfo.Instance.MyDeckMap[MyInfo.Instance.MyCurrentDeck];
//				for(int i=0; i<deckInfo.SlotMap.Count; i++)
//				{
//					if( deckInfo.SlotMap[i].UnitDBID == 0 )
//					{
//						deckInfo.SlotMap[i] = _unitInfo;
//						break;
//					}
//				}
//				manager.RefreshDeckSlots();
				listManager.RefreshCardStatus(this);
			}
			else
			{
				selectedCardEffect.gameObject.SetActive(false);
				alreadySelectedCardMark.gameObject.SetActive(false);
				manager.selectedCardObject.gameObject.SetActive(false);
				isSelected = false;
//				DeckInfo deckInfo = MyInfo.Instance.MyDeckMap[MyInfo.Instance.MyCurrentDeck];
//				for(int i=0; i<deckInfo.SlotMap.Count; i++)
//				{
//					if( deckInfo.SlotMap[i].UnitDBID == _unitInfo.UnitDBID )
//					{
//						deckInfo.SlotMap[i] = new UnitInfo(0,0,0,0,0);
//						break;
//					}
//				}
//				manager.RefreshDeckSlots();
				listManager.RefreshCardStatus(this);
			}
		}
		else if( CSceneManager.m_Scene == SCENE.DeckEditScene )
		{
			LoadingWindow.window.ShowShortLoading();
			CSceneManager.manager.GoToUnitInvenScene();
		}
	}
	
	void OnPress(bool pressed)
	{
		if( CSceneManager.m_Scene == SCENE.DeckEditScene )
		{
			if(!pressed)
				editManager.isDragging = false;
		}
	}
	
	void OnDrag(Vector2 delta)
	{
		if( CSceneManager.m_Scene == SCENE.DeckEditScene )
		{
			editManager.Drag(index, delta); 
		}
	}
}
